package zdream.pmfield.stage.buff.ability;

import zdream.pmfield.data.foe.MoveAdditionalEffects;
import zdream.pmfield.data.foe.Type;
import zdream.pmfield.stage.director.AttackContext;
import zdream.pmfield.stage.director.FixPair;
import zdream.pmfield.stage.director.Hooks;

import static zdream.pmfield.stage.director.HookType.ON_CHECK_TYPE_FIXED;

/**
 * 幽灵族效果:
 * <li>普通系的伤害技能对你无效. (普通对精神系的克制倍率改写为免疫)
 * </li>
 *
 * @author Zdream
 * @date 2023-12-22
 * @since 0.0.1
 */
public class GhostGroupBuff extends AbilityBuff {
	public static final String CATEGORY = MoveAdditionalEffects.A_GHOST_GROUP;

	public GhostGroupBuff(BuffDescription description) {
		super(description);
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	@Override
	public void init() {
		onHook(ON_CHECK_TYPE_FIXED)
				.handle(this::checkTypeFixedWhenIAmDefender)
				.buildAndSave();
	}

	private void checkTypeFixedWhenIAmDefender(Hooks.HookContext rawContext) {
		Hooks.AttackHookContext context = rawContext.as();
		final AttackContext attackContext = context.attackContext;
		final AttackContext.AttackContextItem item = attackContext.findAttackItemForDefender(target);

		if (item == null) {
			return;
		}

		item.typeFixPairs.stream()
				.filter(FixPair::isForType)
				.map(FixPair::asType)
				.filter(pair -> pair.attack == Type.NORMAL)
				.filter(pair -> pair.defense == Type.MIND)
				.forEach(pair -> pair.fixRateLevel = Type.TYPE_FIXED_RATE_LEVEL_IMMUNE);
	}
}
